package com.bawei.module_repair.adapter

import android.graphics.Outline
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.widget.Button
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_common.base.BaseApplication
import com.bawei.module_repair.R
import com.bawei.module_repair.model.RepairModelItem

class RepairAdapter(
    var onItemLoginClick: ((id: Int) -> Unit)? = null,
): RecyclerView.Adapter<RepairAdapter.RepairViewHolder>() {
    private val repairList= mutableListOf<RepairModelItem>()

    fun setRepairData(data:List<RepairModelItem>){
        repairList.clear()
        repairList.addAll(data)
        notifyDataSetChanged()
    }
    override fun getItemViewType(position: Int): Int {
        return position
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RepairViewHolder {
        return RepairViewHolder(View.inflate(parent.context, R.layout.item_repair, null))
    }

    override fun getItemCount(): Int {
        return repairList.size
    }

    override fun onBindViewHolder(holder: RepairViewHolder, position: Int) {
        val repair = repairList[position]
        val colorRes = when (repair.processingStatus) {
            "待处理" -> com.bawei.lib_common.R.color.color_yellow_dark
            "处理中" -> com.bawei.lib_common.R.color.color_3298ff
            "已完成" -> com.bawei.lib_common.R.color.color_40b223
            "已提交" -> com.bawei.lib_common.R.color.color_40b223
            else -> android.R.color.black // 默认颜色
        }
        when (repair.processingStatus) {
            "待处理" -> holder.repairState.setBackgroundResource(R.drawable.shape_item_repair_one)
            "处理中" -> holder.repairState.setBackgroundResource(R.drawable.shape_item_repair_two)
            "已完成" -> holder.repairState.setBackgroundResource(R.drawable.shape_item_repair_there)
            "已提交" -> holder.repairState.setBackgroundResource(R.drawable.shape_item_repair_there)
            else -> android.R.color.black // 默认颜色
        }
        holder.repairState.setTextColor(ContextCompat.getColor(BaseApplication.instance, colorRes))
        holder.repairState.text = repair.processingStatus
        holder.repairNumber.text = "工单号: ${repair.orderNumber}"
        holder.repairTime.text = "报修时间: ${repair.repairTime}"
        holder.repairPlane.text = "位置: ${repair.repairLocation}"
        holder.repairName.text = "报修人: ${repair.repairName}"
        holder.repairDescribe.text = "${repair.faultDescription}"
        holder.repairPhone.text = "联系电话: ${repair.contactMethod}"
        holder.itemView.setOnClickListener {
            onItemLoginClick?.invoke(repair.id)
        }
    }
    class RepairViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val repairState: TextView = view.findViewById(R.id.item_repair_state)
        val  repairNumber: TextView = view.findViewById(R.id.item_repair_number)
        val  repairPlane: TextView = view.findViewById(R.id.item_repair_plane)
        val  repairName: TextView = view.findViewById(R.id.item_repair_name)
        val  repairDescribe: TextView = view.findViewById(R.id.item_repair_describe)
        val  repairTime: TextView = view.findViewById(R.id.item_repair_time)
        val  repairPhone: TextView = view.findViewById(R.id.item_repair_phone)
    }
    fun setupLayoutManager(recyclerView: RecyclerView) {
        val layoutManager = LinearLayoutManager(recyclerView.context)
        recyclerView.layoutManager = layoutManager
//        // 添加间距装饰（水平间距16dp，垂直间距8dp）
        val spacing = recyclerView.context.resources.getDimensionPixelSize(R.dimen.grid_spacing)
        recyclerView.addItemDecoration(GridSpaceItemDecoration(
            horizontalSpacing = spacing,  // 水平间距（左右）
            verticalSpacing = spacing / 2 // 垂直间距（上下，可根据需求调整）
        ))
        // 可选：启用预加载（提升滑动流畅性）
        recyclerView.setItemViewCacheSize(20)
    }
}